//
//  ViewController.swift
//  IDPNativeApp
//
//  Created by 九州云腾 on 16/11/8.
//  Copyright © 2016年 九州云腾. All rights reserved.
//

import UIKit
import STSFramework
/**************************************************************************************/
/***************************************  import  *************************************/
/**************************************************************************************/
import Alamofire
class LoginViewController: UIViewController ,UITextFieldDelegate{

    // 用户名textfield
    @IBOutlet weak var username: UITextField!

    // 密码textfield
    @IBOutlet weak var password: UITextField!

    @IBOutlet weak var version: UILabel!

    @IBOutlet weak var serverIp: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()


        /**************************************************************************************/
        /************************************  打开app行为  *************************************/
        /**************************************************************************************/

        let versionDic =  Bundle.main.infoDictionary! as [String : Any]

        let version :String = versionDic["CFBundleShortVersionString"] as! String

        self.version.text = "Version \(version)"
        if (UserDefaults.standard.object(forKey: "ServerURL") != nil){

            self.serverIp.text = UserDefaults.standard.object(forKey: "ServerURL")as? String

        }

        self.serverIp.addTarget(self, action:#selector(LoginViewController.textFiledChange(textField:)), for: UIControlEvents.editingChanged)








    }



    // 登录按钮点击事件
    @IBAction func loginClick(_ sender: AnyObject) {
//        var parameters = Dictionary<String, Any>()
//        parameters = [
//            "appKey": "4dbfb4bfe9bb43f386d473e789db6776eIE2sxJlE7b",
//            "appSecret": "sVWx3VGXD5qmHTBvM6lWcFvOJOUHh5UTllrc7Z8FPr",
//            "username": self.username.text!,
//            "password": self.password.text!,
//        ]
        if self.username.text!.characters.count == 0
            || self.password.text!.characters.count == 0 {
            Global.showAlertViewWithController(self, title: "失败", and: "用户名和密码信息不能为空", callback: nil);
            return;
        }

        
        Global.showProgressHUD(content: "登录中");
        STSSDK.requestId_token(requestURL: "\(self.serverIp.text!)/api/public/sts/retrieve_id_token", username: self.username.text!, password: self.password.text!, appKey: "4dbfb4bfe9bb43f386d473e789db6776eIE2sxJlE7b", appSercert: "sVWx3VGXD5qmHTBvM6lWcFvOJOUHh5UTllrc7Z8FPr", success: { (result) in
            DispatchQueue.main.async(execute: {

                Global.hideProgressHUD()
                let userVC = Global.instantiateViewControllerFromStoryboardID("UserInfo") as! UserInfoViewController;
                let navigationController: UINavigationController = UINavigationController(rootViewController: userVC);
                navigationController.modalTransitionStyle = .crossDissolve
                userVC.username = result["id_token"] as! String
                userVC.serverIp = self.serverIp.text!
                userVC.isSSO = false
                userVC.refreshToken = result["refresh_token"] as! String
                self.present(navigationController, animated: true, completion: nil);

            })

        }) { (error) in
            Global.hideProgressHUD()
            print(error)
        }


    }


    //MARK: - 保存服务器地址

    func textFieldDidBeginEditing(_ textField: UITextField) {

    }
    func textFiledChange( textField:UITextField) {
        
        UserDefaults.standard.set(textField.text, forKey: "ServerURL")
        UserDefaults.standard.synchronize()
        
    }
    
}

